package com.baway.biliclitent.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baway.biliclitent.R;
import com.baway.biliclitent.model.LiveBean;
import com.baway.biliclitent.util.GlideImageLoader;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * 分区直播适配
 * 王培炎
 */

public class ZoneLiveFragRecyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private LiveBean liveBean;
    private int entranceSize;

    //直播分类入口
    private static final int TYPE_ENTRANCE = 0;

    //直播Item
    private static final int TYPE_LIVE_ITEM = 1;

    //直播分类Title
    private static final int TYPE_PARTITION = 2;

    //直播页Banner
    private static final int TYPE_BANNER = 3;
    private List<String> imageList = new ArrayList<>();
    public ZoneLiveFragRecyAdapter(Context context, LiveBean liveBean) {
        this.context = context;
        this.liveBean = liveBean;
        entranceSize = 4;
        List<LiveBean.DataBean.BannerBean> banner = liveBean.getData().getBanner();
        for (int i = 0; i < banner.size(); i++) {
            imageList.add(banner.get(i).getImg());
        }

    }
    private int[] entranceIconRes = new int[]{
            R.drawable.live_home_follow_anchor,
            R.drawable.live_home_live_center,
            R.drawable.live_home_search_room,
            R.drawable.live_home_all_category
    };

    private String[] entranceTitles = new String[]{
            "关注主播", "直播中心",
            "搜索直播", "全部分类"
    };

    public int getSpanSize(int pos) {

        int viewType = getItemViewType(pos);
        switch (viewType) {
            case TYPE_ENTRANCE:
                return 3;
            case TYPE_LIVE_ITEM:
                return 6;
            case TYPE_PARTITION:
                return 12;
            case TYPE_BANNER:
                return 12;
        }
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ENTRANCE:
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_live_entrance, null);
                return new LiveEntranceViewHolder(view);
            case TYPE_LIVE_ITEM:
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_live_partition, null);
                return new LiveItemViewHolder(view);
            case TYPE_PARTITION:
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_live_partition_title, null);
                return new LivePartitionViewHolder(view);
            case TYPE_BANNER:
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_live_banner, null);
                return new LiveBannerViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        position -= 1;
        final LiveBean.DataBean.PartitionsBean.LivesBeanX livesBean;
        if (holder instanceof LiveEntranceViewHolder) {

            LiveEntranceViewHolder liveEntranceViewHolder = (LiveEntranceViewHolder) holder;
            liveEntranceViewHolder.title.setText(entranceTitles[position]);
            Glide.with(context)
                    .load(entranceIconRes[position])
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(((LiveEntranceViewHolder) holder).image);
        } else if (holder instanceof LiveItemViewHolder) {

            LiveItemViewHolder liveItemViewHolder = (LiveItemViewHolder) holder;

            livesBean = liveBean.getData().getPartitions().get(getItemPosition(position))
                    .getLives().get(position - 1 - entranceSize - getItemPosition(position) * 5);

            Glide.with(context)
                    .load(livesBean.getCover().getSrc())
                    .centerCrop()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.bili_default_image_tv)
                    .dontAnimate()
                    .into(liveItemViewHolder.itemLiveCover);

            Glide.with(context)
                    .load(livesBean.getCover().getSrc())
                    .centerCrop()
                    .dontAnimate()
                    .placeholder(R.drawable.ico_user_default)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(((LiveItemViewHolder) holder).itemLiveUserCover);

            liveItemViewHolder.itemLiveTitle.setText(livesBean.getTitle());
            liveItemViewHolder.itemLiveUser.setText(livesBean.getOwner().getName());
            liveItemViewHolder.itemLiveCount.setText(String.valueOf(livesBean.getOnline()));
            /*liveItemViewHolder.itemLiveLayout.setOnClickListener(v -> LivePlayerActivity.
                    launch((Activity) context, livesBean.getRoom_id(),
                            livesBean.getTitle(), livesBean.getOnline(), livesBean.getOwner().getFace(),
                            livesBean.getOwner().getName(), livesBean.getOwner().getMid()));*/
        } else if (holder instanceof LivePartitionViewHolder) {

            LivePartitionViewHolder livePartitionViewHolder = (LivePartitionViewHolder) holder;

            LiveBean.DataBean.PartitionsBean.PartitionBeanX partition = liveBean.
                    getData().getPartitions().get(getItemPosition(position)).getPartition();

            Glide.with(context)
                    .load(partition.getSub_icon().getSrc())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(livePartitionViewHolder.itemIcon);
            livePartitionViewHolder.itemTitle.setText(partition.getName());
            SpannableStringBuilder stringBuilder = new SpannableStringBuilder(
                    "当前" + partition.getCount() + "个直播");
            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(
                    context.getResources().getColor(R.color.pink_text_color));
            stringBuilder.setSpan(foregroundColorSpan, 2,
                    stringBuilder.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            livePartitionViewHolder.itemCount.setText(stringBuilder);
        } else if (holder instanceof LiveBannerViewHolder) {
            LiveBannerViewHolder liveBannerViewHolder = (LiveBannerViewHolder) holder;
            liveBannerViewHolder.banner.setImageLoader(new GlideImageLoader());
            liveBannerViewHolder.banner.setImages(imageList);
            liveBannerViewHolder.banner.setIndicatorGravity(BannerConfig.RIGHT);
            liveBannerViewHolder.banner.start();
        }
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return TYPE_BANNER;
        }
        position -= 1;
        if (position < entranceSize) {
            return TYPE_ENTRANCE;
        } else if (isPartitionTitle(position)) {
            return TYPE_PARTITION;
        } else {
            return TYPE_LIVE_ITEM;
        }
    }

    /**
     * 获取当前Item在第几组中
     */
    private int getItemPosition(int pos) {

        pos -= entranceSize;
        return pos / 5;
    }


    private boolean isPartitionTitle(int pos) {

        pos -= entranceSize;
        return (pos % 5 == 0);
    }

    @Override
    public int getItemCount() {
        if (liveBean != null) {
            return 1 + entranceIconRes.length
                    + liveBean.getData().getPartitions().size() * 5;
        } else {
            return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }

    /**
     * 直播界面Banner ViewHolder
     */
    static class LiveBannerViewHolder extends RecyclerView.ViewHolder {

        public Banner banner;
        LiveBannerViewHolder(View itemView) {
            super(itemView);
            banner = (Banner) itemView.findViewById(R.id.item_live_banner);
        }
    }

    /**
     * 直播界面Item分类 ViewHolder
     */
    static class LiveEntranceViewHolder extends RecyclerView.ViewHolder {

        public TextView title;
        public ImageView image;


        LiveEntranceViewHolder(View itemView) {

            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.live_entrance_image);
            title = (TextView) itemView.findViewById(R.id.live_entrance_title);
        }
    }

    /**
     * 直播界面Grid Item ViewHolder
     */
    static class LiveItemViewHolder extends RecyclerView.ViewHolder {

        ImageView itemLiveCover;

        TextView itemLiveUser;

        TextView itemLiveTitle;

        com.baway.biliclitent.view.CircleImageView itemLiveUserCover;

        TextView itemLiveCount;

        CardView itemLiveLayout;


        LiveItemViewHolder(View itemView) {
            super(itemView);
            itemLiveCover = (ImageView) itemView.findViewById(R.id.item_live_cover);
            itemLiveUser = (TextView) itemView.findViewById(R.id.item_live_user);
            itemLiveTitle = (TextView) itemView.findViewById(R.id.item_live_title);
            itemLiveUserCover = (com.baway.biliclitent.view.CircleImageView) itemView.findViewById(R.id.item_live_user_cover);
            itemLiveCount = (TextView) itemView.findViewById(R.id.item_live_count);
            itemLiveLayout = (CardView) itemView.findViewById(R.id.item_live_layout);
        }
    }

    /**
     * 直播界面分区类型 ViewHolder
     */
    static class LivePartitionViewHolder extends RecyclerView.ViewHolder {

        ImageView itemIcon;
        TextView itemTitle;
        TextView itemCount;

        LivePartitionViewHolder(View itemView) {

            super(itemView);
            itemIcon = (ImageView) itemView.findViewById(R.id.item_live_partition_icon);
            itemTitle = (TextView) itemView.findViewById(R.id.item_live_partition_title);
            itemCount = (TextView) itemView.findViewById(R.id.item_live_partition_count);
        }
    }

}
